<?php


namespace addons\facrm\controller;



use fast\Date;
use think\addons\Controller;

/**
 * 云呼回调接口
 * @icon fa fa-circle-o
 */
class Cloudcall extends Controller
{
    protected $cacheCallName = "cache_call_engine_name";
    protected $calltypes = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->request->filter(['strip_tags']);

    }

    /**
     * 通话记录回调
     * @ApiParams(name="engine", type="string", required=true, description="通道")
     * @ApiBody ("域名/addons/facrm/cloudcall/back?engine=通道(如Tycc100)")
     * @ApidBody
     */
    public function back($engine=null)
    {
        $engine=!$engine?$this->request->request('engine'):$engine;
		$engine=!$engine?$this->request->route('engine'):"";
        if (!$engine){
            $this->error("通道不存在");
        }
        $this->setting = cache($this->cacheCallName.$engine);

        if (!$this->setting) {
            $this->key = "cloudcall";
            $settingModel = new \app\admin\model\facrm\Setting();
            $this->setting=$settingModel->where('key',  "cloudcall".$engine)->find();
            if (!$this->setting){
                $this->error("通道不存在");
            }
            $this->setting=$this->setting->toArray();
            cache($this->cacheCallName.$engine,  $this->setting, 86400);//缓存一天
        }

        $this->setting['key'] = $this->setting['describe'];
        $this->setting = array_merge($this->setting, $this->setting['values']);
        $call= \addons\facrm\library\cloudcall\Call::instance($this->setting);
        $result=$call->back();

    }


}
